<template>
  <scroll-view class="flex-col page mine" scroll-y @scroll="onPageScroll">
    <view class="flex-col section" :style="{ paddingTop: navHeight + 'px' }">
      <xlm-top-nav :scrollTop="scrollTop" title="报名" />
      <view class="registration-page">
        <!-- 顶部横幅 -->
        <view class="banner-section">
          <image class="banner-image"
            :src="eventData.base.master_image ? eventData.base.master_image : imgUrl + 'a.jpg'" mode="aspectFill" />
          <view class="banner-overlay">
            <!-- <text class="banner-title">活动报名</text> -->
          </view>
        </view>

        <!-- 活动信息卡片 -->
        <view class="event-card">
          <view class="event-thumbnail">
            <image class="thumbnail-image" :src="eventData.base.master_image" mode="aspectFill" />
            <!-- <text class="thumbnail-text">BADMINTON</text> -->
          </view>
          <view class="event-info">
            <text class="event-title">{{ eventData.base.name }}</text>
            <text class="event-time">比赛时间: {{ formatTime(eventData.base.competition_time) }}</text>
          </view>
        </view>
        <view class="athlete-bar">
          <view class="athlete-info">
            <view class="athlete-avatar">
              <image :src="currentPlayer?.avatar || imgUrl + 'avatar1@2x.png'" mode="aspectFill" />
            </view>
            <view class="athlete-details">
              <text class="athlete-name">{{ currentPlayer ? currentPlayer.username : '请选择运动员' }}</text>
              <text class="athlete-mobile">{{ currentPlayer?.mobile || 'N/A' }}</text>
            </view>
          </view>
          <view class="switch-btn" @click="switchAthlete">
            <text class="switch-text">切换</text>
            <image class="switch-icon" :src="imgUrl + 'switch@2x.png'" mode="aspectFit" />
          </view>
        </view>
        <!-- 项目选择 -->
        <view class="section-card">
          <view class="section-header">
            <text class="section-title">选择项目</text>
            <view style="display: flex;flex-direction:column;align-items:flex-end;">
              <text class="section-limit">一个人最多只能报一项</text>
              <!-- <text class="section-limit">最多只能参加4个项目</text> -->
              <!-- <text class="section-limit">个人项目最多报2个，团体接力项目最多报2个</text> -->
            </view>

          </view>
          <!-- <view class="project-list">
            <view v-for="project in projectList" :key="project.item_id" class="project-item"
              :class="{ 'selected': selectedProjects.includes(project.item_id) }"
              @click="toggleProject(project.item_id)">
              <view class="project-radio">
                <view class="radio-circle" :class="{ 'checked': selectedProjects.includes(project.item_id) }">
                  <text v-if="selectedProjects.includes(project.item_id)" class="radio-check">✓</text>
                </view>
              </view>
              <text class="project-name">{{ project.item_name }}</text>
              <view class="project-action">
                <text class="action-text">请选项目 ></text>
              </view>
            </view>
          </view> -->
          <scroll-view class="project-list" scroll-y @scrolltolower="loadMoreProjects" style="height: 800rpx;">
            <view v-for="project in projectList" :key="project.item_id" class="project-item" style="margin-bottom: 24rpx;"
              :class="{ 'selected': selectedProjects.includes(project.item_id) }"
              @click="toggleProject(project.item_id)">
              <view class="project-radio">
                <view class="radio-circle" :class="{ 'checked': selectedProjects.includes(project.item_id) }">
                  <text v-if="selectedProjects.includes(project.item_id)" class="radio-check">✓</text>
                </view>
              </view>
              <text class="project-name">{{ project.item_name }}</text>
              <view class="project-action">
                <text class="action-text">请选项目 ></text>
              </view>
            </view>
            <view v-if="loading" class="loading" style="text-align: center;margin-top: 20rpx;font-size: 20rpx;">加载中...
            </view>
            <!-- <view v-if="noMore" class="no-more" style="text-align: center;margin-top: 20rpx;font-size: 20rpx;"  >没有更多项目了</view> -->
          </scroll-view>


        </view>

        <!-- 赛事包选择 -->
        <!-- <view class="section-card">
          <view class="section-header">
            <text class="section-title">赛事包选择</text>
          </view>
          <view class="package-section">
            <view class="package-action" @click="selectPackagePopup">
              <text class="action-text">
                {{ selectedPackage ? getSelectedPackageSize() : '请选择' }}
              </text>
              <text class="action-arrow">></text>
            </view>
          </view>
        </view> -->

        <!-- 俱乐部选择 -->
        <view class="section-card">
          <view class="section-header">
            <text class="section-title">俱乐部</text>
          </view>
          <view class="club-options">
            <view class="club-option" @click="selectClub('none')">
              <view class="radio-circle" :class="{ 'checked': selectedClub === 'none' }">
                <text v-if="selectedClub === 'none'" class="radio-check">✓</text>
              </view>
              <text class="club-text">无</text>
            </view>
            <view class="club-option" @click="selectClub('join')">
              <view class="radio-circle" :class="{ 'checked': selectedClub === 'join' }">
                <text v-if="selectedClub === 'join'" class="radio-check">✓</text>
              </view>
              <text class="club-text">加入俱乐部</text>
            </view>
          </view>

          <!-- 俱乐部分享码输入框 -->
          <view v-if="selectedClub === 'join'" class="club-code-section">
            <input class="code-input" v-model="clubShareCode" placeholder="请输入俱乐部分享码" maxlength="20" />
          </view>
        </view>

        <!-- 支付方式 -->
        <!-- <view class="section-card">
          <view class="section-header">
            <text class="section-title">支付方式</text>
          </view>
          <view class="payment-options">
            <view class="payment-option" @click="selectPayment('wxpay')">
              <view class="radio-circle" :class="{ 'checked': selectedPayment === 'wxpay' }">
                <text v-if="selectedPayment === 'wxpay'" class="radio-check">✓</text>
              </view>
              <text class="payment-text">微信支付</text>
            </view>
            <view class="payment-option" @click="selectPayment('ecoin')">
              <view class="radio-circle" :class="{ 'checked': selectedPayment === 'ecoin' }">
                <text v-if="selectedPayment === 'ecoin'" class="radio-check">✓</text>
              </view>
              <text class="payment-text">E币支付</text>
              <text class="ecoin-balance">可用: {{ EcoinList.total_ecoins }}</text>
            </view>
          </view>
        </view> -->

        <!-- 优惠券 -->
        <!-- <view class="section-card">
          <view class="coupon-section">
            <text class="coupon-title">优惠券</text>
            <view class="coupon-action" @click="selectCoupon">
              <text class="action-text">
                {{ selectedCoupon ? selectedCoupon.name : '请选择' }}
              </text>
              <text class="action-arrow">></text>
            </view>
          </view>
        </view> -->

        <!-- 费用结算 -->
        <!-- <view class="section-card">
          <view class="cost-section">
            <text class="cost-title">费用结算</text>
            <text class="cost-amount">¥{{ totalCost }}</text>
          </view>
        </view> -->

        <!-- 底部提交按钮 -->
        <view class="bottom-actions">
          <button class="submit-btn" @click="submitRegistration">
            提交报名
          </button>
        </view>
      </view>
    </view>

    <!-- 运动员选择弹窗 -->
    <uni-popup ref="athletePopup" type="bottom" :safe-area="true">
      <view class="athlete-popup">
        <view class="popup-header">
          <text class="popup-title">选择运动员</text>
          <view class="close-btn" @click="closeAthletePopup">
            <text class="close-text">×</text>
          </view>
        </view>
        <view class="popup-content">
          <view v-if="playerList.length === 0" class="empty-state">
            <text class="empty-text">暂无运动员信息</text>
          </view>
          <view v-else class="player-list">
            <view v-for="player in playerList" :key="player.id" class="player-item"
              :class="{ 'selected': currentPlayer && currentPlayer.id === player.id }" @click="selectPlayer(player)">
              <view class="player-avatar">
                <image :src="player.avatar || imgUrl + 'avatar1@2x.png'" mode="aspectFill" />
              </view>
              <view class="player-info">
                <text class="player-name">{{ player.username }}</text>
                <text class="player-id">手机号: {{ player.mobile || 'N/A' }}</text>
              </view>
              <view class="player-status">
                <view v-if="currentPlayer && currentPlayer.id === player.id" class="status-selected">
                  <text class="status-text">已选择</text>
                </view>
                <view v-else class="status-unselected">
                  <text class="status-text">选择</text>
                </view>
              </view>
            </view>
          </view>
        </view>
      </view>
    </uni-popup>
    <!-- 优惠券选择弹窗 -->
    <uni-popup ref="couponPopup" type="bottom" :safe-area="true">
      <view class="coupon-popup">
        <view class="popup-header">
          <text class="popup-title">选择优惠券</text>
          <view class="close-btn" @click="closeCouponPopup">
            <text class="close-text">×</text>
          </view>
        </view>
        <view class="popup-content">
          <view v-if="couponList.length === 0" class="empty-state">
            <text class="empty-text">暂无可用优惠券</text>
          </view>
          <view v-else class="coupon-list">
            <view v-for="coupon in couponList" :key="coupon.id" class="coupon-item"
              :class="{ 'selected': selectedCoupon && selectedCoupon.id === coupon.id }"
              @click="selectCouponItem(coupon)">
              <!-- 金额区 -->
              <view class="coupon-amount">
                <view class="amount">￥{{ coupon.discount_price }}</view>
                <view class="limit">满{{ coupon.small_price }}可用</view>
              </view>
              <!-- 信息区 -->
              <view class="coupon-info">
                <view class="coupon-title">{{ coupon.name }}</view>
                <view class="coupon-date">有效期：{{ formatTimeOnly(coupon.starttime) }} 至 {{ formatTimeOnly(coupon.endtime)
                }}</view>
              </view>
              <!-- 选择状态 -->
              <!-- <view class="coupon-status">
                <view v-if="selectedCoupon && selectedCoupon.id === coupon.id" class="status-selected">
                  <text class="status-text">已选择</text>
                </view>
                <view v-else-if="!coupon.is_available" class="status-disabled">
                  <text class="status-text">不可用</text>
                </view>
                <view v-else class="status-available">
                  <text class="status-text">选择</text>
                </view>
              </view> -->
            </view>
          </view>
        </view>
      </view>
    </uni-popup>
    <!-- 赛事包选择弹窗 -->
    <uni-popup ref="packagePopup" type="bottom" :safe-area="true">
      <view class="package-popup">
        <view class="popup-header">
          <text class="popup-title">选择赛事包</text>
          <view class="close-btn" @click="closePackagePopup">
            <text class="close-text">×</text>
          </view>
        </view>
        <view class="popup-content">
          <view v-if="packageList.length === 0" class="empty-state">
            <text class="empty-text">暂无可用赛事包</text>
          </view>
          <view v-else class="package-list">
            <view v-for="(pkg, index) in packageList" :key="pkg.id" class="package-item"
              :class="{ 'selected': selectedPackage === pkg.id }" @click="selectPackageItem(pkg.id)">
              <view class="package-radio">
                <view class="radio-circle" :class="{ 'checked': selectedPackage === pkg.id }">
                  <text v-if="selectedPackage === pkg.id" class="radio-check">✓</text>
                </view>
              </view>
              <view class="package-content">
                <view class="package-info">
                  <text class="package-name">{{ pkg.size }}</text>
                  <text class="package-source">{{ pkg.source || '系统' }}</text>
                </view>
              </view>
              <view class="package-status">
                <view v-if="selectedPackage === pkg.id" class="status-selected">
                  <text class="status-text">已选择</text>
                </view>
                <view v-else class="status-available">
                  <text class="status-text">选择</text>
                </view>
              </view>
            </view>
          </view>
        </view>
      </view>
    </uni-popup>
  </scroll-view>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useOutsideAppStore } from '@/store/app'
import { imgUrl } from '@/utils/config'
import { formatTime, formatTimeOnly } from '@/utils/dateUtils.js'
import { onShow } from '@dcloudio/uni-app';
import { useRequest } from '@/api';
const { API_INDEX_DETAIL_POST, API_MEMBER_LISTATHLETE_POST, API_EVENT_ITEMS_POST, API_EVENT_BAG_POST, API_MEMBER_MYECOIN_POST, API_DISCOUNT_MY_POST, API_INDEX_CREATE_POST, API_INDEX_PAY_POST } = useRequest()
const appStore = useOutsideAppStore()
const navHeight = computed(() => appStore.navHeight)
const scrollTop = ref(0)
const EcoinList = ref({})
// 弹窗引用
const athletePopup = ref(null)
const couponPopup = ref(null)
const packagePopup = ref(null)
const onPageScroll = (e) => {
  scrollTop.value = e.detail.scrollTop
}

// 响应式数据
const eventData = ref({
  base: {
    name: '',
    competition_time: '',
    master_image: ''
  },
  group: [],
  club: [],
  course: [],
  score: []
})
const playerList = ref([])
const currentPlayer = ref(null) // 当前选中的运动员
const projectList = ref([
])
const loading = ref(false);
const noMore = ref(false);
const currentPage = ref(1);
const pageSize = 10; // 每页项目数量
const packageList = ref([
  {
    id: 'uniform',
    name: '田径服',
    source: '系统',
    image: imgUrl + 'a.jpg'
  }
])

const selectedProjects = ref([])
const selectedPackage = ref('')
const selectedClub = ref('')
const selectedPayment = ref('wxpay')
const ecoinBalance = ref(0)
const clubShareCode = ref('')
// 新增：优惠券相关
const couponList = ref([])
const selectedCoupon = ref(null)
const event_id_params = ref({}) // 赛事id
// 计算属性
const totalCost = computed(() => {
  let cost = 0
  // 项目费用
  cost += selectedProjects.value.length * 20
  // 赛事包费用
  if (selectedPackage.value) {
    cost += 79
  }
  return cost.toFixed(2)
})

const canSubmit = computed(() => {
  return selectedProjects.value.length > 0 && selectedPayment.value
})

// 方法
const toggleProject = (projectId) => {
  const index = selectedProjects.value.indexOf(projectId)
  // if (index > -1) {
  //   selectedProjects.value.splice(index, 1)
  // } else {
  //   if (selectedProjects.value.length < 4) {
  //     selectedProjects.value.push(projectId)
  //   } else {
  //     uni.showToast({
  //       title: '最多只能选择4个项目',
  //       icon: 'none'
  //     })
  //   }
  // }
  if (index > -1) {
    selectedProjects.value.splice(index, 1)
  } else {
    if (selectedProjects.value.length < 1) {
      selectedProjects.value.push(projectId)
    } else {
      uni.showToast({
        title: '最多只能选择1个项目',
        icon: 'none'
      })
    }
  }
}

// const selectPackage = (packageId) => {
//   selectedPackage.value = selectedPackage.value === packageId ? null : packageId
// }

const selectClub = (clubType) => {
  selectedClub.value = clubType
  if (clubType == 'join') {
  } else {
    clubShareCode.value = null
  }
}

const selectPayment = (paymentType) => {
  selectedPayment.value = paymentType
}

const selectCoupon = () => {
  if (couponList.value.length === 0) {
    uni.showToast({
      title: '暂无可用优惠券',
      icon: 'none'
    });
    return;
  }
  // 打开优惠券选择弹窗
  if (couponPopup.value) {
    couponPopup.value.open();
  }
}
// 新增：选择优惠券
const selectCouponItem = (coupon) => {
  if (coupon.status == 'used') {
    uni.showToast({
      title: '该优惠券已使用',
      icon: 'none'
    });
    return;
  }
  if (coupon.status == 'expired') {
    uni.showToast({
      title: '该优惠券已过期',
      icon: 'none'
    });
    return;
  }
  // if(coupon.status == 'notuse'){
  //   uni.showToast({
  //     title: '该优惠券未使用',
  //     icon: 'none'
  //   });
  //   return;
  // }
  selectedCoupon.value = coupon;
  // 关闭弹窗
  closeCouponPopup();
  uni.showToast({
    title: `已选择优惠券: ${coupon.name}`,
    icon: 'success'
  });
}
// 新增：打开赛事包选择弹窗
const selectPackagePopup = () => {
  if (packageList.value.length === 0) {
    uni.showToast({
      title: '暂无可用赛事包',
      icon: 'none'
    });
    return;
  }
  // 打开赛事包选择弹窗
  if (packagePopup.value) {
    packagePopup.value.open();
  }
}

// 新增：选择赛事包
const selectPackageItem = (packageId) => {
  selectedPackage.value = packageId;
  // 关闭弹窗
  closePackagePopup();
  uni.showToast({
    title: `已选择赛事包`,
    icon: 'success'
  });
}

// 新增：关闭赛事包选择弹窗
const closePackagePopup = () => {
  if (packagePopup.value) {
    packagePopup.value.close();
  }
}

// 新增：获取已选择赛事包的size
const getSelectedPackageSize = () => {
  const selected = packageList.value.find(pkg => pkg.id === selectedPackage.value);
  return selected ? selected.size : '';
}
// 新增：关闭优惠券选择弹窗
const closeCouponPopup = () => {
  if (couponPopup.value) {
    couponPopup.value.close();
  }
}
const submitRegistration = async () => {

  // 基础校验
  if (!currentPlayer.value || !currentPlayer.value.id) {
    uni.showToast({ title: '请选择运动员', icon: 'none' })
    return
  }
  if (selectedProjects.value.length === 0) {
    uni.showToast({ title: '请选择项目', icon: 'none' })
    return
  }
  // if (!selectedPayment.value) {
  //   uni.showToast({ title: '请选择支付方式', icon: 'none' })
  //   return
  // }

  const params = {
    id: event_id_params.value, // 赛事id
    athlete_id: currentPlayer.value.id ? currentPlayer.value.id : null, // 运动员id
    items: selectedProjects.value ? selectedProjects.value.join(',') : '', // 项目id数组
    eventbag_id: selectedPackage.value ? selectedPackage.value : '', // 赛事包id
    code: clubShareCode.value ? clubShareCode.value : '', // 俱乐部分享码
    mode: selectedPayment.value ? selectedPayment.value : '', // 支付方式
    discountmember_id: selectedCoupon.value ? selectedCoupon.value.id : '', // 优惠券id（可选）
  }
  try {

    const res = await API_INDEX_CREATE_POST(params)
    console.log('submitRegistration', res)

    if (res.code === 1) {
      uni.showToast({ title: "报名成功", icon: 'none' })
      setTimeout(() => {
        uni.navigateBack()
      }, 1500)
      // payOrder(res.data.id)
    } else {
      uni.showToast({ title: res.msg, icon: 'none' })
    }

  } catch (error) {
    uni.showToast({ title: error.message, icon: 'none' })
  }
}
const payOrder = async (id) => {
  let params = {
    id: id
  }
  try {

    const res = await API_INDEX_PAY_POST(params)
    console.log('payOrder', res)

    if (res.code === 1) {
      uni.showToast({ title: '支付成功', icon: 'success' })
      setTimeout(() => {
        uni.navigateBack()
      }, 1500)
    } else {
      uni.showToast({ title: res.msg, icon: 'none' })
    }

  } catch (error) {
    uni.showToast({ title: error.message, icon: 'none' })
  }
}
onShow(async () => {
  const pages = getCurrentPages(); // 获取当前页面栈
  const currentPage = pages[pages.length - 1]; // 获取当前页面
  console.log('currentPage', currentPage);

  const options = currentPage.options; // 获取当前页面的参数
  const { event_id } = options;
  event_id_params.value = event_id;
  if (event_id) {
    getEventDetail(event_id)//获取报名页基础数据
  }
  getPlayerList();// 获取运动员列表
  getEventItems(event_id, currentPage.value, pageSize);//获取赛事项目
  getEventPackage();//获取赛事包
  getMyEcoin();//获取我的E币
  getMyCoupon('notuse');//获取我的优惠券--未使用
})
const getEventDetail = async (id) => {
  const longitude = uni.getStorageSync('longitude')
  const latitude = uni.getStorageSync('latitude')
  let params = {
    id: id,
    lng: longitude,
    lat: latitude
  }
  eventData.value = {
    base: {
      name: '',
      competition_time: '',
      master_image: ''
    },
    group: [],
    club: [],
    course: [],
    score: []
  }
  try {

    const res = await API_INDEX_DETAIL_POST(params)
    console.log('getEventDetail', res)

    if (res.code === 1) {
      eventData.value = res.data;
    } else {
      uni.showToast({ title: res.msg, icon: 'none' })
    }
  } catch (error) {
    uni.showToast({ title: error.message, icon: 'none' })
  }

}
const getPlayerList = async () => {
  let params = {
    page: 1,
    page_size: 10,
    lng: uni.getStorageSync('longitude'),
    lat: uni.getStorageSync('latitude')
  }
  playerList.value = [];
  try {

    const res = await API_MEMBER_LISTATHLETE_POST(params)
    console.log('getPlayerList', res)

    if (res.code === 1) {
      if (res.data.data.length > 0) {
        playerList.value = res.data.data;
        // 设置默认选中的运动员为第一个
        currentPlayer.value = res.data.data[0];
      } else {
        playerList.value = [];
        currentPlayer.value = null;
      }
    } else {
      uni.showToast({ title: res.msg, icon: 'none' })
    }
    console.log('playerList', playerList.value)
  } catch (error) {
    uni.showToast({ title: error.message, icon: 'none' })
  }
}
const switchAthlete = () => {
  if (playerList.value.length === 0) {
    uni.showToast({
      title: '暂无运动员信息',
      icon: 'none'
    });
    return;
  }
  // 打开运动员选择弹窗
  if (athletePopup.value) {
    athletePopup.value.open();
  }
}

// 选择运动员
const selectPlayer = (player) => {
  currentPlayer.value = player;
  // 关闭弹窗
  closeAthletePopup();
  uni.showToast({
    title: `已选择运动员: ${player.username}`,
    icon: 'success'
  });
}

// 关闭运动员选择弹窗
const closeAthletePopup = () => {
  if (athletePopup.value) {
    athletePopup.value.close();
  }
}
const getEventPackage = async () => {
  let params = {
    // page:1,
    // page_size:10
  }
  packageList.value = [];
  try {
    const res = await API_EVENT_BAG_POST(params)
    console.log('getEventPackage', res)
    if (res.code === 1) {
      packageList.value = res.data;
    } else {
      uni.showToast({ title: res.msg, icon: 'none' })
    }
    console.log('packageList', packageList.value)
  } catch (error) {
    uni.showToast({ title: error.message, icon: 'none' })
  }
}
// const getEventItems = async (event_id) => {
//   let params = {
//     event_id: event_id
//   }
//   projectList.value = [];
//   try {
//     const res = await API_EVENT_ITEMS_POST(params)
//     console.log('getEventItems', res)
//     if (res.code === 1) {
//       projectList.value = res.data.data;
//     } else {
//       uni.showToast({ title: res.msg, icon: 'none' })
//     }
//     console.log('projectList++++', projectList.value)
//   } catch (error) {
//     uni.showToast({ title: error.message, icon: 'none' })
//   }
// }
const getEventItems = async (event_id, page, pageSize) => {
  let params = {
    event_id: event_id,
    page: page,
    page_size: pageSize
  };

  try {
    const res = await API_EVENT_ITEMS_POST(params);
    console.log('getEventItems', res);

    if (res.code === 1) {
      // 如果是第一页，直接赋值；否则追加数据
      if (page === 1) {
        projectList.value = res.data.data;
      } else {
        projectList.value = [...projectList.value, ...res.data.data];
      }

      // 检查是否有更多数据
      if (res.data.data.length < pageSize) {
        noMore.value = true;
      } else {
        noMore.value = false;
      }
    } else {
      uni.showToast({ title: res.msg, icon: 'none' });
    }

    console.log('projectList++++', projectList.value);
  } catch (error) {
    uni.showToast({ title: error.message, icon: 'none' });
  }
};
// 加载更多数据的函数
const loadMoreProjects = async () => {
  if (loading.value || noMore.value) return;
  loading.value = true;
  currentPage.value++;

  try {
    await getEventItems(event_id_params.value, currentPage.value, pageSize);
  } finally {
    loading.value = false;
  }
};
const getMyEcoin = async () => {
  let params = {
    page: 1,
    page_size: 10
  }
  EcoinList.value = {
    total_ecoins: 0,
    not_credited: 0
  }
  try {

    const res = await API_MEMBER_MYECOIN_POST(params)
    console.log('getMyEcoin', res)

    if (res.code === 1) {
      if (res.data) {
        EcoinList.value = res.data;
      } else {
        EcoinList.value = {};
      }
      console.log('EcoinList', EcoinList.value);

    } else {
      uni.showToast({ title: res.msg, icon: 'none' })
    }
    console.log('EcoinList', EcoinList.value);

  } catch (error) {
    uni.showToast({ title: error.message, icon: 'none' })
  }
}
const getMyCoupon = async (type) => {
  let params = {
    page: 1,
    page_size: 10,
    type: type
  }
  couponList.value = []
  try {

    const res = await API_DISCOUNT_MY_POST(params)
    console.log('getMyCoupon', res)

    if (res.code === 1) {
      if (res.data.data.length > 0) {
        couponList.value = res.data.data;
      } else {
        couponList.value = [];
      }
    } else {
      uni.showToast({ title: res.msg, icon: 'none' })
    }
    console.log('couponList', couponList.value);

  } catch (error) {
    uni.showToast({ title: error.message, icon: 'none' })
  }
}
</script>

<style lang="scss" scoped>
@import './index.scss';
</style>